<template>
  <div class="radar-chart-wrapper">
    <div ref="radarChart" class="chart-container"></div>
    <div v-if="loading" class="chart-loading">
      <el-icon class="is-loading"><Loading /></el-icon>
      <span>加载中...</span>
    </div>
    <div v-if="!loading && isEmpty" class="chart-empty">
      <el-empty description="暂无数据" />
    </div>
  </div>
</template>

<script setup>
import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
import * as echarts from 'echarts'
import { Loading } from '@element-plus/icons-vue'

const props = defineProps({
  // 图表标题
  title: {
    type: String,
    default: ''
  },
  // 静态数据
  data: {
    type: Array,
    default: () => []
  },
  // 指标配置
  indicators: {
    type: Array,
    required: true
  },
  // 图表高度
  height: {
    type: String,
    default: '350px'
  },
  // 是否显示图例
  showLegend: {
    type: Boolean,
    default: true
  },
  // 自定义颜色
  colors: {
    type: Array,
    default: () => [
      '#5470c6',
      '#91cc75',
      '#fac858',
      '#ee6666',
      '#73c0de'
    ]
  },
  // 雷达图形状：polygon(多边形), circle(圆形)
  shape: {
    type: String,
    default: 'polygon',
    validator: (value) => ['polygon', 'circle'].includes(value)
  },
  // 是否填充区域
  areaStyle: {
    type: Boolean,
    default: true
  },
  // 动画效果
  animation: {
    type: Boolean,
    default: true
  }
})

const radarChart = ref(null)
let chartInstance = null
const loading = ref(false)
const isEmpty = ref(false)

// 初始化图表配置
const getOption = () => {
  return {
    color: props.colors,
    title: props.title ? {
      text: props.title,
      left: 'center',
      top: '10',
      textStyle: {
        fontSize: 16,
        fontWeight: 'bold',
        color: '#333'
      }
    } : null,
    tooltip: {
      trigger: 'item',
      backgroundColor: 'rgba(50, 50, 50, 0.9)',
      borderColor: '#333',
      borderWidth: 1,
      textStyle: {
        color: '#fff'
      }
    },
    legend: {
      show: props.showLegend,
      bottom: '10',
      left: 'center',
      textStyle: {
        fontSize: 12,
        color: '#666'
      }
    },
    radar: {
      indicator: props.indicators,
      shape: props.shape,
      center: ['50%', '55%'],
      radius: '65%',
      splitNumber: 4,
      name: {
        textStyle: {
          fontSize: 12,
          color: '#666'
        }
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(0, 0, 0, 0.1)'
        }
      },
      splitArea: {
        show: true,
        areaStyle: {
          color: ['rgba(114, 172, 209, 0.05)', 'rgba(114, 172, 209, 0.1)']
        }
      },
      axisLine: {
        lineStyle: {
          color: 'rgba(0, 0, 0, 0.2)'
        }
      }
    },
    series: [
      {
        type: 'radar',
        data: props.data.map((item, index) => ({
          value: item.value,
          name: item.name,
          areaStyle: props.areaStyle ? {
            opacity: 0.3
          } : null,
          lineStyle: {
            width: 2
          },
          symbol: 'circle',
          symbolSize: 6
        })),
        emphasis: {
          lineStyle: {
            width: 4
          },
          areaStyle: props.areaStyle ? {
            opacity: 0.6
          } : null
        }
      }
    ],
    animation: props.animation
  }
}

// 初始化图表
const initChart = () => {
  if (!radarChart.value) return

  // 如果实例已存在，先销毁
  if (chartInstance) {
    chartInstance.dispose()
  }

  chartInstance = echarts.init(radarChart.value)
  chartInstance.setOption(getOption())

  // 添加窗口大小改变监听
  window.addEventListener('resize', handleResize)
}

// 处理窗口大小改变
const handleResize = () => {
  if (chartInstance) {
    chartInstance.resize()
  }
}

// 加载数据
const loadData = () => {
  if (!props.data || props.data.length === 0) {
    isEmpty.value = true
    return
  }

  isEmpty.value = false
  nextTick(() => {
    initChart()
  })
}

// 监听数据变化
watch(() => props.data, () => {
  loadData()
}, { deep: true })

onMounted(() => {
  loadData()
})

onUnmounted(() => {
  if (chartInstance) {
    window.removeEventListener('resize', handleResize)
    chartInstance.dispose()
    chartInstance = null
  }
})

// 暴露刷新方法
defineExpose({
  refresh: loadData,
  getInstance: () => chartInstance
})
</script>

<style scoped lang="scss">
.radar-chart-wrapper {
  position: relative;
  width: 100%;
  height: v-bind(height);

  .chart-container {
    width: 100%;
    height: 100%;
  }

  .chart-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    color: #999;
    font-size: 14px;

    .el-icon {
      font-size: 32px;
    }
  }

  .chart-empty {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
  }
}
</style>

